package com.example.firstopengl.programs;

import android.content.Context;
import android.opengl.GLES20;

import com.example.firstopengl.R;

/**
 * Created by liqian-ps on 2017/7/6.
 */

public class TextureShaderProgram extends ShaderProgram {

    // uniform location
    private int uMatrixLocation;
    private int uTextureUnitLocation;

    //attribute localtion
    private int aPositionLocation;
    private int aTextureCoordinatesLocation;

    public TextureShaderProgram(Context context) {
        super(context, R.raw.texture_vertex_shader, R.raw.texture_fragment_shader);
        uMatrixLocation = GLES20.glGetUniformLocation(programId, U_MATRIX);
        uTextureUnitLocation = GLES20.glGetUniformLocation(programId, U_TEXTURE_UNIT);

        aPositionLocation = GLES20.glGetAttribLocation(programId, A_POSITION);
        aTextureCoordinatesLocation = GLES20.glGetAttribLocation(programId, A_TEXTURECOORDINATES);
    }


    public void setUniforms(float[] matrix, int textureId){
        GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);

        GLES20.glUniform1i(uTextureUnitLocation, 0);
    }

    public int getPositionAttributeLocation(){
        return aPositionLocation;
    }
    public int getTextureCoordinatesLocation(){
        return aTextureCoordinatesLocation;
    }

}
